package net.begincode.servlet.blog;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import utils.BQStringUtil;
import utils.BadWords;
import utils.HtmlUtil;
import utils.JsoupUtil;
import utils.ViewCount;

import com.google.gson.Gson;

import net.begincode.bean.BcBlog;
import net.begincode.bean.BcClassify;
import net.begincode.bean.BcUser;
import net.begincode.servlet.common.BaseServlet;
import net.begincode.servlet.common.ViewServlet;

public class BlogServlet extends BaseServlet{
	private static final Log LOG = LogFactory.getLog(BlogServlet.class);
	   
	/**
	 * 获取用户登录信息
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	public void blogInit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		Map<String, String> userMap = this.getCookieInfo(request, response);
		BcBlog bcBlog = new BcBlog();
		bcBlog.setBc_user_id(Integer.parseInt(userMap.get("bcUserId"))); 
		request.setAttribute("blogs", this.blogService.selBlogByParam(bcBlog));
		request.getRequestDispatcher("/WEB-INF/page/blogManager.jsp").forward(request, response);
	}
	
	/**
	 * 新增博客初始化
	 * @throws UnsupportedEncodingException 
	 * */
	public void addBlogInit(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{
		this.selClassToAttr(request, response);
		try {
			request.getRequestDispatcher("/WEB-INF/page/editBlog.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 新增保存博客
	 * @throws UnsupportedEncodingException 
	 * */
	public void saveBlog(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{
		BcBlog bcBlog = new BcBlog();
		bcBlog.setBc_classify_id(Integer.valueOf(request.getParameter("classify")));
		bcBlog.setBlog_title(request.getParameter("blogTitle"));
		bcBlog.setBlog_abstract(request.getParameter("abstract"));
		bcBlog.setBlog_content(request.getParameter("content"));
		bcBlog.setBlog_keyword(request.getParameter("bckeyword"));
		Map<String,String> cookMap = this.getCookieInfo(request, response);
		bcBlog.setBc_user_id(Integer.valueOf(cookMap.get("bcUserId").toString()));
		bcBlog.setBc_user_name(cookMap.get("nickName"));
		bcBlog.setCheck_flag("0");
		bcBlog.setDiscuss_flag("1");
		bcBlog.setPraise(0);
		bcBlog.setSite_recommend("");
		bcBlog.setView_count(0);
		bcBlog.setWonderfu("0");
		String blogId = request.getParameter("blogId");
		if(blogId.equals("")){
			bcBlog = blogService.addBlog(bcBlog);
			if(bcBlog != null){
				request.setAttribute("msg", "保存成功");
			}
		}else{
			bcBlog.setBc_blog_id(Integer.parseInt(blogId));
			if(blogService.updBlog(bcBlog) > 0){
				request.setAttribute("msg", "保存成功");
			}
		}
		request.setAttribute("blog", bcBlog);
		try {
			this.selClassToAttr(request, response);
			request.getRequestDispatcher("/WEB-INF/page/editBlog.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void checkBlogInfo(HttpServletRequest request, HttpServletResponse response){
		
	}
	
	/**
	 * 查询登录人博客，浏览排行前n
	 * */
	
	public void selBlogAjaxLimitN(HttpServletRequest request, HttpServletResponse response){
		List<BcBlog> blogs = new ArrayList<BcBlog>();
		try {
			String userId = request.getParameter("userId");
			if(userId != null && !userId.equals("")){
				blogs = this.blogService.selBlogLimitN(Integer.parseInt(userId), 10);
			}else{
				Map userMap = this.getCookieInfo(request, response);
				blogs = this.blogService.selBlogLimitN(Integer.parseInt(userMap.get("bcUserId").toString()), 10);
			}
			Gson gs = new Gson();
			response.getWriter().print(gs.toJson(blogs));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	/**
	 * 修改博客初始化
	 * @throws UnsupportedEncodingException 
	 * */
	public void updBlogInit(HttpServletRequest request, HttpServletResponse response) {
		this.selClassToAttr(request, response);
		String blogId = request.getParameter("blogId");
		request.setAttribute("blog", this.blogService.selBlogById(Integer.parseInt(blogId)));
		try {
			request.getRequestDispatcher("/WEB-INF/page/editBlog.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//查询当前用户博客分类并存储
	private void selClassToAttr(HttpServletRequest request, HttpServletResponse response)  {
		Map userMap;
		try {
			userMap = this.getCookieInfo(request, response);
			List<BcClassify> classifyList = this.classifyService.selClassifyByUserId(userMap.get("bcUserId").toString());
			request.setAttribute("classifyList", classifyList);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * blog查看
	 * */
	public void selBlogView(HttpServletRequest request, HttpServletResponse response){
		String blogId = request.getAttribute("id").toString();
		ViewCount.addViewCount(ViewServlet.VIEW_BLOG+":"+blogId); //添加阅读数
		BcBlog bcBlog = this.blogService.selBlogByIdCache(Integer.parseInt(blogId));
		request.setAttribute("blog",bcBlog );
		request.setAttribute("blogUserId", bcBlog.getBc_user_id());
		request.setAttribute("blogUserName", bcBlog.getBc_user_name());
		try {
			request.getRequestDispatcher("/WEB-INF/page/blogview.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
